package org.upc.edu.AIAExamples.communication;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;

import java.util.ArrayList;

public class Casa extends Agent {
	
	long EAcumulada;
	ArrayList<Long> ultimsbalans;
	long mitjanabalans;
	FuncioGauss EProduida;
	FuncioGauss EConsumida;
	FuncioGauss Ingressos;
	long diners;
	
	
	public class buyerbehaviour extends CyclicBehaviour{
		public buyerbehaviour(Agent agent) {
			super(agent);
		}

		@Override
		public void action() {
			ACLMessage msg = receive();
			if(msg==null) return; //debug
			if(msg==null) {
				// si cal iniciem comerÃ§ entre cases
			}
			if(msg.getSender().getLocalName().equals("clock")) {
				
			}
			
		}
	}
	
	public class sellerbehaviour extends CyclicBehaviour{
		public sellerbehaviour(Agent agent) {
			super(agent);
		}

		@Override
		public void action() {
			ACLMessage msg = receive();
			if(msg==null) return; //debug
			if(msg==null) {
				// si cal iniciem comerÃ§ entre cases
			}
			if(msg.getSender().getLocalName().equals("clock")) {
				
			}
			
		}
	}
	
	
	public class casabehaviour extends CyclicBehaviour
    {
		public casabehaviour(Agent agent) {
			super(agent);
		}

		public void action() {
			ACLMessage msg = receive();
			if(msg==null) return; //debug
			if(msg==null) {
				// si cal iniciem comerÃ§ entre cases
			}
			if(msg.getSender().getLocalName().equals("clock")) {
				if (EAcumulada < 0){
					System.out.println(myAgent.getLocalName()+": Energia negativa, enviant peticio de " + -EAcumulada);
					ACLMessage peticio = new ACLMessage();
					peticio.setSender(myAgent.getAID());
					AID central = new AID();
			        central.setLocalName("central");
					peticio.addReceiver(central);
					peticio.setContent(String.valueOf(-EAcumulada));
					myAgent.send(peticio);
				}
				long balans = 0;
				balans += EProduida.next();
				balans -= EConsumida.next();
				if(ultimsbalans.size()>=10) ultimsbalans.remove(0);
				ultimsbalans.add(balans);
				EAcumulada += balans;
				mitjanabalans = mitjanabalans();
				diners += Ingressos.next();
				//negociacio
				
				System.out.println(myAgent.getLocalName()+": tick rebut, balanÃ§: "+balans+" Energia actual: "+EAcumulada);
				
			}
			else if(msg.getSender().getLocalName().equals("central")) {	
				EAcumulada += Long.parseLong(msg.getContent());
				diners -= Long.parseLong(msg.getContent()) * 8;
				System.out.println(myAgent.getLocalName()+": Energia rebuda: "+msg.getContent()+" Energia Actual: "+EAcumulada);
			}
			else {
				// peticions comerÃ§ entre cases
			}
		}
    }
	
	protected void setup() {
		Object[] args = getArguments();
		System.out.println("creant casa amb parametres: "+args[0].toString()+" "+args[1].toString()+" "+args[2].toString()+" "+args[3].toString());
		EConsumida = new FuncioGauss(Long.parseLong(args[0].toString()),Long.parseLong(args[1].toString()));
		EProduida = new FuncioGauss(Long.parseLong(args[2].toString()),Long.parseLong(args[3].toString()));
		EAcumulada = 0;
		ultimsbalans = new ArrayList<Long>();
		mitjanabalans = 0;
		Ingressos = new FuncioGauss(Long.parseLong(args[4].toString()),Long.parseLong(args[5].toString()));
		diners = 0;
		/*ACLMessage peticio = new ACLMessage();
		peticio.setSender(this.getAID());
		AID central = new AID();
        central.setLocalName("central");
		peticio.addReceiver(central);
		peticio.setContent(args[0].toString());
		this.send(peticio);*/
		casabehaviour b = new casabehaviour(this);
		this.addBehaviour(b);
	}

	public long mitjanabalans() {
		long mitja = 0;
		for(long l: ultimsbalans) {
			mitja += l;
		}
		if (mitja > 0) mitja = 0;
		return -mitja/ultimsbalans.size();
	}
}
